package com.nj.api.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nj.api.entity.Account;
import com.nj.api.entity.User;
import com.nj.api.exception.MsgTipException;
import com.nj.api.mapper.AccountMapper;
import com.nj.api.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Value("${salt}")
    private String salt;
    @Override
    public boolean checkExists(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Long aLong = userMapper.selectCount(wrapper);
        if (aLong==1){
            return true;
        }
        return false;
    }

    @Override
    public Map register(String phone, String password, String code) {//注册
        HashMap<String, Object> map = new HashMap<>();
        checkData(phone,password,code);//后端对数据格式校验

        password = salt+password;
        password= DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        User user = new User();
        user.setPhone(phone);
        user.setLoginPassword(password);
        userMapper.insert(user);

        //注册后自动开户
        User u1 = getUserByPhone(user.getPhone());
        accountMapper.insert(new Account().setUid(u1.getId()));

        map.put("success", true);

        return map;
    }

    @Override
    public void checkData(String phone, String password, String code) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String realCode = (String) valueOperations.get("code:register:" + phone);
        if (code==null||realCode==null||!realCode.equals(code)||!code.matches("^(\\d{4,6})$")){
            throw  new MsgTipException("验证码不正确");
        } else if (password==null||!password.matches("^(?=.*\\d)(?=.*[a-zA-Z])[\\da-zA-Z]{6,20}$")) {
            throw  new MsgTipException("密码不正确");
        } else if (phone==null||!phone.matches("^1[3-9]\\d{9}$")||checkExists(phone)) {
            throw  new MsgTipException("手机号码不正确");
        }
    }

    @Override
    public User login(String phone, String password) {
        password = salt+password;
        String password2 = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone).eq("login_password",password2);
        User user = userMapper.selectOne(wrapper);
        //修改最近登录时间
        user.setLastLoginTime(LocalDateTime.now());
        userMapper.updateById(user);
        if (user==null){
            throw new MsgTipException("账号或密码不正确");
        }
        return user;
    }

    @Override
    public User getUserByPhone(String phone) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
        if (user==null)throw  new MsgTipException("用户名不存在");
        return user;
    }


}
